// @flow
import React from 'react'
import { Form, Input, Button, message } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
const FormItem = Form.Item
import styles from './AMQEdit.css'
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}
const tailFormItemLayout = {
  wrapperCol: {
    xs: {
      span: 24,
      offset: 0
    },
    sm: {
      span: 14,
      offset: 6
    }
  }
}

type Props = {
  history: Object,
  form: Object
}

type State = {
  loading: boolean,
  taskData: Object
}

class AMQEdit extends React.Component {
  props: Props
  state: State
  submit: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      loading: false,
      taskData: {}
    }

    this.submit = this.submit.bind(this)
  }

  componentWillMount () {
    const { data } = this.props.history.location.state
    this.setState({
      taskData: data
    })
  }

  submit () {
    const { form } = this.props
    form.validateFields((err, fieldsValue) => {
      if (err) {
        return false
      }
      this.setState({
        loading: true
      })
      const values = {
        ...fieldsValue
      }
      const { taskData } = this.state
      universalFetch(`${__SERVICE_API__}sys/amq/${taskData.id}`,
        {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          },
          body: JSON.stringify(values)
        })
        .then((res) => res.json())
        .then((json) => {
          if (json.code !== 200000) {
            throw new Error(JSON.stringify({ code: json.code, message: json.message }))
          }
          this.setState({
            loading: false
          })
          message.success('修改成功')
          this.props.history.push('/system/amqSetting')
        })
        .catch((error) => {
          handleFetchError(error)
          console.error(error)
          message.error('修改失败')
          this.setState({
            loading: false
          })
        })
    })
  }

  render () {
    const { getFieldDecorator } = this.props.form
    const { taskData } = this.state
    return (
      <div className={styles['main']}>
        <Form onSubmit={this.handleSubmit}>
          <FormItem {...formItemLayout} label='地址' hasFeedback>
            {
              getFieldDecorator('addressIp', {
                rules: [
                  { required: true, message: '请填写地址', whitespace: true }
                ],
                initialValue: taskData.addressIp
              })(
                <Input placeholder='请填写地址' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='端口' hasFeedback>
            {
              getFieldDecorator('addressPort', {
                rules: [
                  { required: true, message: '请填写端口', whitespace: true }
                ],
                initialValue: taskData.addressPort
              })(
                <Input placeholder='请填写端口' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='topic' hasFeedback>
            {
              getFieldDecorator('topic', {
                rules: [
                  { required: true, message: '请填写topic', whitespace: true }
                ],
                initialValue: taskData.topic
              })(
                <Input placeholder='请填写topic' />
              )
            }
          </FormItem>
          <FormItem
            {...tailFormItemLayout}
          >
            <Button type='primary'
              className='login-form-button'
              onClick={this.submit}
              >
              提交
            </Button>
            <Button style={{ marginLeft: '20px' }}
              onClick={() => {
                this.props.history.push('/system/amqSetting')
              }}
              >
              返回
            </Button>
          </FormItem>
        </Form>
      </div>
    )
  }
}
export default Form.create()(AMQEdit)
